<template>
  <!-- 来两个盒子 -->
  <!--  表单元素:
                 表单验证
                          1:表单元素v-model绑定
                          2:el-form
                            model:整个表单元素的值
                            rules:{
                              prop的值:[
                              {required:true,message:'错误信息提醒',trigger:'blur/change},
                              {min:最小长度,max:最大长度,message:"错误信息提醒",trigger:'blur/change},
                              {pattern:正则表达式,message:"错误信息提醒",trigger:"blur/change"}
                              ]
                            } -->
  <div class="login">
    <div class="box">
      <img src="@/assets/common/login-logo.png" alt="" />
      <el-form ref="form" class="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input
            v-model="form.mobile"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入账号"
            @keyup.enter.native="submit"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-unlock"
            placeholder="请输入密码"
            show-password
            @keyup.enter.native="submit"
          ></el-input>
        </el-form-item>
        <el-button class="btn" type="primary" @click="submit" :loading="loading"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          {
            //  全局表单验证
            // pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            // message: '请输入正确的手机号',
            // trigger: 'change'
            //  自定义表单验证
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请输入手机号'))
              } else {
                if (
                  !/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                    value
                  )
                ) {
                  callback(new Error('请输入正确的手机号2'))
                } else {
                  callback()
                }
              }
            }
          }
        ],
        password: [
          //  全局表单验证
          // { min: 6, max: 20, message: '请输入6-20位密码', trigger: 'change' }
          {
            //  自定义表单验证
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请输入密码2'))
              } else {
                if (value.length < 6 || value.length > 20) {
                  callback(new Error('请输入6-20位密码2'))
                } else {
                  callback()
                }
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        /* 
         调用接口获取token并存储
         1:api/login.js  定义api
         2：login/index.vue 导入
         3: 表单验证通过后调用
         4: 调用成功存储token到vuex的user2模块中
         */
        if (result) {
          // try {
          // const res = await SysLogin(this.form)
          // this.$store.commit('user2/setToken', res.data.data)
          this.loading = true
          try {
            console.log(1)
            await this.$store.dispatch('user2/toLogin', this.form)
            console.log(2)
            this.$message.success('登录成功')
            //  登录页接收回跳地址
            this.$router.push(this.$route.query.back || '/')
          } catch {
            this.loading = false
          }
          // console.log(res)
          // } catch {}
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  //  导入图片
  width: 100%;
  height: 100%;
  background-image: url('~@/assets/common/login.jpg');
  // 溢出裁切
  display: flex;
  background-size: cover;
  justify-content: center;
  align-items: center;
  .box {
    // 登录框
    // 边框1px
    width: 500px;
    // height: 500px;
    // border: 1px solid;
    // background-color: #fff;
    text-align: center;
    padding: 6px;
    .form {
      // 与log间距保持为20px
      margin-top: 30px;
      ::v-deep .el-form-item__error {
        color: #fff;
      }
    }
  }
  .btn {
    width: 100%;
    background-color: #407ffe;
  }
}
</style>
